package org.desktop.web.client.timesheet.window;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.desktop.web.client.custom.widget.AbstractWindow;
import org.desktop.web.client.custom.widget.AsyncYearTree;
import org.desktop.web.client.timesheet.form.TimesheetFormPanel;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ibm.icu.text.DateFormat;
import com.sencha.gxt.core.client.util.DateWrapper;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.DatePicker;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.DatePicker.DatePickerAppearance;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer.BorderLayoutData;

public class TimesheetWindow extends AbstractWindow {

	private AsyncYearTree yearTree; 
	
	private ContentPanel panel;
	
	public TimesheetWindow() {
		super();
		this.setWidth(790);
		this.setHeight(480);
		this.panel=new ContentPanel();
		this.panel.setHeaderVisible(false);
		BorderLayoutContainer con = new BorderLayoutContainer();
		
		ContentPanel westPanel = new ContentPanel();
		
		westPanel.setHeaderVisible(false);
		//pannello che contiene i giorni del mese
		ContentPanel centerPanel = new ContentPanel();
		centerPanel.setHeaderVisible(false);
		this.yearTree = new AsyncYearTree(centerPanel);

		
		
//		westPanel.add(this.yearTree.asWidget());
		
		final ContentPanel tempCenterPanel = centerPanel;
		
		DatePicker datePicker = new DatePicker(){

			@Override
			public void setValue(Date date) {
				// TODO Auto-generated method stub
//				com.google.gwt.user.client.Window.alert("TimesheetWindow.TimesheetWindow().new DatePicker() {...}.setValue(date)");
				super.setValue(date);
			}

			@Override
			public void setValue(Date date, boolean fireEvents) {
				// TODO Auto-generated method stub
//				com.google.gwt.user.client.Window.alert("TimesheetWindow.TimesheetWindow().new DatePicker() {...}.setValue("+date+","+fireEvents+")");
				if (!fireEvents) {
					tempCenterPanel.clear();
					tempCenterPanel.add(new TimesheetFormPanel().asWidget());
				}

				super.setValue(date, fireEvents);
			}
			
		};
//		GregorianCalendar gMinDate = (GregorianCalendar) GregorianCalendar.getInstance();
//		GregorianCalendar gMaxDate = (GregorianCalendar) GregorianCalendar.getInstance();
//		
//		gMinDate.set(Calendar.MONTH, 0);
//		gMinDate.set(Calendar.DAY_OF_MONTH, 1);
//		gMinDate.set(Calendar.YEAR, 2014);
//		gMaxDate.set(Calendar.MONTH, 0);
//		gMaxDate.set(Calendar.DAY_OF_MONTH, 31);
//		gMaxDate.set(Calendar.YEAR, 2014);
//		
//		
//		datePicker.setMinDate(new DateWrapper().addDays(-11).asDate());
//		datePicker.setMaxDate(new Date(2014,0,31));
		datePicker.setEnabled(true);
		datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<Date> event) {
				// TODO Auto-generated method stub
				Date d = event.getValue();
				tempCenterPanel.clear();
				tempCenterPanel.add(new TimesheetFormPanel().asWidget());
				
			}
		});
		
		
		
		datePicker.setAllowTextSelection(true);
		westPanel.add(datePicker);
		centerPanel.add(new TimesheetFormPanel().asWidget());
		
		BorderLayoutData bld = new BorderLayoutData();
		bld.setSplit(true);
		bld.setSize(0.3);
		con.setWestWidget(westPanel,bld);
		con.setCenterWidget(centerPanel,bld);
		this.panel.add(con);
		this.add(panel);
		// TODO Auto-generated constructor stub
	}

	@Override
	public String id() {
		// TODO Auto-generated method stub
		return "TIMESHEET";
	}
	
	

}
